package com.dy.common.utils.cms;

import com.alibaba.fastjson2.JSON;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
/**
 * 阿里云短信服务：
 * 注意：需要 签名名称、模版CODE 以及 RAM访问控制中的 AccessKeyID 和 AccessKeySecret
 */
public class AliyunSmsUtils {

    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
    static final String accessKeyId = "LTAI5tLdxxQrrrbgvakJqCJg";  // TODO 修改成自己的
    static final String accessKeySecret = "mWTI54dzlNYTYnf39IF0WtiVHJVYh9";   // TODO 修改成自己的


    public static void sendBatchSms(String templateCode,String phone,Map<String,Object> templateParamMap)throws Exception{
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                ).build();

        SendSmsRequest sendSmsRequest = SendSmsRequest.builder().phoneNumbers(phone).signName("鼎耀环保")
        .templateCode(templateCode).templateParam(JSON.toJSONString(templateParamMap)).build();
        System.out.println(JSON.toJSON(sendSmsRequest));

        // Asynchronously get the return value of the API request
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }


    public static void main(String[] args)throws Exception {
        Map<String,Object> map = new HashMap<>();
        map.put("code",123456);
        sendBatchSms("SMS_246015060","18883876319",map);
    }


    /***
     * 添加短信签名
     * @throws Exception
     */
    public static void addSmsSign(AddSmsSignRequest signRequest)throws Exception {
        // Configure Credentials authentication information, including ak, secret, token
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                ).build();

        CompletableFuture<AddSmsSignResponse> response = client.addSmsSign(signRequest);
        AddSmsSignResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }

    /***
     * 删除短信签名
     * @param deleteSmsSignRequest
     * @throws Exception
     */
    public static void deleteSmsSign(DeleteSmsSignRequest deleteSmsSignRequest)throws Exception{
        // Configure Credentials authentication information, including ak, secret, token
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder().region("cn-hangzhou") // Region ID
                .credentialsProvider(provider).
                        overrideConfiguration(ClientOverrideConfiguration.create().setEndpointOverride("dysmsapi.aliyuncs.com")).build();
        try {
            CompletableFuture<DeleteSmsSignResponse> response = client.deleteSmsSign(deleteSmsSignRequest);
            DeleteSmsSignResponse resp = response.get();
            System.out.println(new Gson().toJson(resp));
        }finally {
            client.close();
        }
    }


    /***
     * 修改短信模板
     * @param modifySmsSignRequest
     * @throws Exception
     */
    public static void updateSmsSign(ModifySmsSignRequest modifySmsSignRequest)throws Exception{
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                ).build();
        CompletableFuture<ModifySmsSignResponse> response = client.modifySmsSign(modifySmsSignRequest);
        ModifySmsSignResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }

    /***
     * 短信签名列表查询
     * @param querySmsSignListRequest
     */
    public static void querySmsSignList(QuerySmsSignListRequest querySmsSignListRequest)throws Exception{
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                ).build();

        CompletableFuture<QuerySmsSignListResponse> response = client.querySmsSignList(querySmsSignListRequest);
        QuerySmsSignListResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }


    /**
     * 添加短信模板
     * @param addSmsTemplateRequest
     * @throws Exception
     */
    public static void addSmsTemplate(AddSmsTemplateRequest addSmsTemplateRequest)throws Exception{
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                ).build();
        CompletableFuture<AddSmsTemplateResponse> response = client.addSmsTemplate(addSmsTemplateRequest);
        AddSmsTemplateResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        // Finally, close the client
        client.close();
    }

    /***
     * 删除短信模板
     * @param deleteSmsTemplateRequest
     * @throws Exception
     */
    public static void deleteSmsTemplate(DeleteSmsTemplateRequest deleteSmsTemplateRequest)throws Exception{
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                ).build();
        CompletableFuture<DeleteSmsTemplateResponse> response = client.deleteSmsTemplate(deleteSmsTemplateRequest);
        DeleteSmsTemplateResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }


    /***
     * 修改未审核通过的模板
     * @param modifySmsTemplateRequest
     * @throws Exception
     */
    public static void modifySmsTemplate(ModifySmsTemplateRequest modifySmsTemplateRequest)throws Exception{
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                ).build();

        CompletableFuture<ModifySmsTemplateResponse> response = client.modifySmsTemplate(modifySmsTemplateRequest);
        ModifySmsTemplateResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));

        client.close();
    }

    /***
     * 查询短信模板列表
     * @throws Exception
     */
    public static QuerySmsTemplateListResponse querySmsTemplateList(QuerySmsTemplateListRequest querySmsTemplateListRequest)throws Exception{
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                ).build();

        CompletableFuture<QuerySmsTemplateListResponse> response = client.querySmsTemplateList(querySmsTemplateListRequest);
        QuerySmsTemplateListResponse resp = response.get();
        System.out.println("-----"+new Gson().toJson(resp));
        client.close();
        return resp;
    }


    /***
     * 查询短信发送统计信息
     * @param querySendStatisticsRequest
     * @throws Exception
     */
    public static void querySendStatistics(QuerySendStatisticsRequest querySendStatisticsRequest)throws Exception{
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(accessKeyId)
                .accessKeySecret(accessKeySecret)
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                ).build();
        CompletableFuture<QuerySendStatisticsResponse> response = client.querySendStatistics(querySendStatisticsRequest);
        QuerySendStatisticsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }


    /***
     * 查询短信发送详情
     * @param querySendDetailsRequest
     * @throws Exception
     */
    public static void QuerySendDetails(QuerySendDetailsRequest querySendDetailsRequest)throws Exception{
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("<your-accessKeyId>")
                .accessKeySecret("<your-accessKeySecret>")
                .build());

        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou") // Region ID
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                ).build();
        CompletableFuture<QuerySendDetailsResponse> response = client.querySendDetails(querySendDetailsRequest);
        QuerySendDetailsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }




}
